
import Vue from 'vue'
import axios from 'axios'
import {
    VueAxios
} from './axios'
import notification from 'ant-design-vue/es/notification'

//使能跨域
axios.defaults.withCredentials = false;
// 创建 axios 实例
const service = axios.create({
    // baseURL: '/api', // 开发环境
    baseURL: '/', // 生产环境
    timeout: 6000 // 请求超时时间
})

const err = (error) => {
    if (error.response) {
        const data = error.response.data
        if (error.response.status === 403) {
            notification.error({
                message: 'Forbidden',
                description: data.message
            })
        }
        if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
            notification.error({
                message: 'Unauthorized',
                description: 'Authorization verification failed'
            })
        }
    }
    return Promise.reject(error)
}

// response interceptor
service.interceptors.response.use((response) => {
    return response.data
}, err)

const installer = {
    vm: {},
    install (Vue) {
        Vue.use(VueAxios, service)
    }
}

export {
    installer as VueAxios,
    service as axios
}